package com.fuego.creditsystemcommodity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuego.creditsystemcommodity.mapper.ActivityMapper;
import com.fuego.creditsystemcommodity.service.IActivityInfoService;
import com.fuego.creditsystemcommodity.service.IActivityService;
import jakarta.annotation.Resource;
import org.example.dto.Result;
import org.example.entity.Activity;
import org.example.entity.ActivityInfo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.example.utils.RedisConstants.ACTIVITY_INFO_STOCK_KEY;


@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {


    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IActivityInfoService activityInfoService;

    @Override
    public Result queryActivityOfOrganizer(Long organizerId) {
        // 查询优惠券信息
        List<Activity> activities = this.getBaseMapper().queryActivityOfOrganizer(organizerId);
        // 返回结果
        return Result.ok(activities);
    }

    @Override
    @Transactional
    public void addActivityInfo(ActivityInfo activityInfo) {

        // 保存秒杀信息
        activityInfoService.save(activityInfo);
        //保存秒杀库存到redis中
        stringRedisTemplate.opsForValue().set(ACTIVITY_INFO_STOCK_KEY+activityInfo.getId(),activityInfo.getStock().toString());
    }

    @Override
    public Result queryAllActivity() {
        List<Activity> vouchers = getBaseMapper().queryAllActivity();
        // 返回结果
        return Result.ok(vouchers);
    }

    @Override
    public Result queryActivityById(Long activityId) {
        Activity activity = getBaseMapper().selectById(activityId);
        return Result.ok(activity);
    }
}
